import {getYMD,getNowFormatDate,getDay,formatGMTDate,sortBy,toPercent} from '../../../../assets/js/util.js' 

import echarts from 'echarts'

export default{
  data() {
    return{
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e7;
        }
      },
      // 默认参数 和 URL
      SRC: '',
      DP: {
        p: '',
        f: '',
        uid: ''
      },
      loading: false,
      searchable: false,
      prop: '',
      order: '',      
      title:'国家分析',
      xlsxTitle:'',
      ruleForm :{
        sday:'',
        eday:'',
        startDate: ''
      },
      tableHead :[{"prop":"countryid","label":"国家"},{"prop":"allNewRole","label":"总新增"},{"prop":"np","label":"国家比例"}],
      tableData: [],
      /*当前页显示数量*/
      pageSize: 10,
      /*每页显示的数量*/
      pageSizes: [10, 20, 50, 100],
      /*dataTotal表数量*/
      dataTotal: 0,
      /*分页器*/
      currentPage: 1,
    }
  }, 
  computed:{
    // xlsx() {
    //     return this.title + '.xlsx'
    // }
  },
  methods: {
    dateChange(val){
      if(val){
        var val = val.split(" ");
        this.sday = val[0];
        this.eday = val[2];
      }
    },
    // 表格下载
    excel(tableIndex,sheet) {
        this.xlsxTitle = sheet+"-"+this.sday+"到"+this.eday;
        this.downloadExl(this.tableHead, this.tableData, this.xlsxTitle,sheet);
    },
    agentChange(){

    },
    attrChange(){

    },
    channelChange(){

    },
    settlWayChange(){

    },
    adPlaceChange(){

    },
    loads() {
      this.loading = !this.loading; //加载中
      this.searchable = !this.searchable; //查询按钮不可用
    },
    search(){
      this.loads();
      this.apiAjax(this.sday,this.eday)
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.dataTotal = this.tableData.length;
      this.tableData= this.tableData.slice((this.currentPage-1)*(this.pageSize),this.currentPage*this.pageSize);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.dataTotal = this.tableData.length;
      this.tableData = this.tableData.slice((this.currentPage-1)*(this.pageSize),this.currentPage*this.pageSize);
    },
    edit (){

    },
    handleDelete (){

    },
    findData (data){
      this.sday = formatGMTDate(new Date(new Date().getTime() - 1000*60*60*24*8),true,true);
      this.eday = formatGMTDate(new Date(new Date().getTime() - 1000*60*60*24*1),true,true);
      this.ruleForm.startDate = [this.sday,this.eday];
      this.loads();
      this.apiAjax(this.sday,this.eday)
    },
    apiAjax(sday,eday){
      this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`,{
        params: {'ps':`dstart=${sday}&dend=${eday}`}
      }).then(function(res){
        var data = eval('(' + JSON.parse(res.body) + ')');

        if(parseInt(data['res']) === 0){
            this.loads();
            var len = data.countryGameData.length;
            this.tableData = [];
            for(var i=0;i<len;i++){
                var obj = {}
                obj["countryid"] = data.countryGameData[i].countryid;
                obj["allNewRole"] = data.countryGameData[i].countryNewRole;
                obj["np"] = toPercent(data.countryGameData[i].countryNewRole/data.countryGameData[i].allNewRole)
               this.tableData.push(obj)
            }
            this.dataTotal = this.tableData.length;

        }else{
           this.errorFun(data['msg'])
        }
      }).catch(function(res){})
    }
  },
  mounted() {
    // 初始化URL
    this.SRC = this.$store.getters.src;
    this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
    this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
    this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
    this.findData(0);

  }
}